package edu.ucdavis.gwt.gis.client.extras;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.dom.client.Style.Float;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.ucdavis.gwt.gis.client.extras.CloseButton;

public class LogoutPopup extends PopupPanel {
	
	private HTML description = new HTML();
	private Anchor link = new Anchor();
	private VerticalPanel vp = new VerticalPanel();
	
	public LogoutPopup() {
		
		Window.addResizeHandler(new ResizeHandler(){
			@Override
			public void onResize(ResizeEvent event) {
				if( isShowing() ) center();
			}
		});
		
		setAutoHideEnabled(true);
		setStyleName("InfoPopup");
		setWidth("400px");
		
		link.setText("Logout of the BZPP Map Viewer");
		
		link.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Cookies.removeCookie("__bzpp_auth");
				Window.Location.replace(link.getHref());
			}
			
		});
		
		vp.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		
		CloseButton close = new CloseButton();
		close.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				hide();
			}
		});

		vp.add(close);
		vp.setCellHorizontalAlignment(close, VerticalPanel.ALIGN_RIGHT);
		close.getElement().getStyle().setMargin(-5, Unit.PX);
		close.getElement().getStyle().setFloat(Float.RIGHT);
		
		vp.add(description);
		vp.add(link);
		
		setWidget(vp);
	}
	
	public void addCloseButton() {
		Button close = new Button("Close");
		close.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				hide();
			}
		});
		vp.add(close);
	}
	
	public void alignDescriptionLeft() {
		vp.setCellHorizontalAlignment(description, VerticalPanel.ALIGN_LEFT);
	}
	
	public void setDescription(String des){
		description.setHTML(des);
	}
	
	
	public void setLinkUrl(String url){
		link.setHref(url);
		if( url.length() == 0 ) link.setVisible(false);
	}
	
}
